package com.wendy.job.controller;

import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.job.domain.bo.SysJobLogBo;
import com.wendy.job.domain.vo.SysJobLogExcelVo;
import com.wendy.job.domain.vo.SysJobLogVo;
import com.wendy.job.entity.SysJobLog;
import com.wendy.job.service.ISysJobLogService;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 *
 * @author wendy
 */
@RestController
@RequestMapping("/job/log")
public class SysJobLogController extends BaseController {
    @Autowired
    private ISysJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @RequiresPermissions("monitor:job:list")
    @GetMapping("/page")
    public TableDataInfo page(SysJobLogBo sysJobLog) {
        TableDataInfo<SysJobLogVo> list = jobLogService.selectJobLogPage(sysJobLog);
        return list;
    }

    /**
     * 导出定时任务调度日志列表
     */
    @RequiresPermissions("monitor:job:export")
    @Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobLogBo sysJobLog) {
        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
        List<SysJobLogExcelVo> result = MapstructUtils.convert(list, SysJobLogExcelVo.class);
        ExcelUtil<SysJobLogExcelVo> util = new ExcelUtil<SysJobLogExcelVo>(SysJobLogExcelVo.class);
        util.exportExcel(response, result, "调度日志");
    }

    /**
     * 根据调度编号获取详细信息
     */
    @RequiresPermissions("monitor:job:query")
    @GetMapping(value = "/{jobLogId}")
    public SysJobLogVo getInfo(@PathVariable Long jobLogId) {
        SysJobLogVo vo = MapstructUtils.convert(jobLogService.selectJobLogById(jobLogId), SysJobLogVo.class);
        return vo;
    }

    /**
     * 删除定时任务调度日志
     */
    @RequiresPermissions("monitor:job:remove")
    @Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobLogIds}")
    public int remove(@PathVariable Long[] jobLogIds) {
        return jobLogService.deleteJobLogByIds(jobLogIds);
    }

    /**
     * 清空定时任务调度日志
     */
    @RequiresPermissions("monitor:job:remove")
    @Log(title = "调度日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public boolean clean() {
        jobLogService.cleanJobLog();
        return true;
    }
}
